package resource.i18n.model;

import java.util.List;

import cn.dreampie.orm.Model;
import cn.dreampie.orm.annotation.Table;

/**
 * Created by hadong on 15-1-22.
 */
@Table(name = "i18_i18n", generatedKey = "id", cached = true)
public class I18n extends Model<I18n> {
	public final static I18n dao = new I18n();

	private String selectSql = "select ii.*,it.name as 'type_name',iv.name as 'version_name',su0.username as 'create_by',su1.username as 'update_by' ";
	private String fromSql = "from i18_i18n ii left join i18_type it on it.id = ii.type_id left join i18_version iv on iv.id = ii.version_id left join sec_user su0 on su0.id = ii.create_id left join sec_user su1 on su1.id = ii.update_id ";

	public List<I18n> findAllI18n() {
		return I18n.dao.find(selectSql + fromSql);
	}

	public I18n findI18nById(Integer id) {
		return I18n.dao.findFirst(selectSql + fromSql + "where ii.id = ?", id);
	}

	public List<I18n> findI18nByTypeId(Integer tid) {
		return I18n.dao.find(selectSql + fromSql + "where ii.type_id = ?", tid);
	}

	public List<I18n> findI18nByVersionId(Integer vid) {
		return I18n.dao.find(selectSql + fromSql + "where iv.version_id = ?", vid);
	}

	public I18n findI18nByLabelNameAndTypeId(String labelName, Integer typeId) {
		return I18n.dao.findFirstBy("label_name = ? and type_id = ?", labelName, typeId);
	}

	public List<I18n> searchI18n(String search) {
		return I18n.dao.findBy("label_name like ? or tags like ?", "%" + search + "%", "%" + search + "%");
	}

}
